<?php
include 'templateSys/SQL.php';
include 'templateSys/Settings.php';
include 'templateSys/manager_util.php';
include 'templateSys/Admin.php';

//loginCheck
checkUserFinal();

global $sideBarResolution;
$sideBarResolution = "editIntResolution.php";
global $link;
$link = getLink();


//nacist historii
if(isset($_POST['id']) && isset($_POST['loadHistory'])){
    $query = sprintf(SQL::$select_intHistory_byId,
        mysql_real_escape_string($_POST['id'])
    );
    $result = mysql_query($query,$link);
    while ($row = mysql_fetch_assoc($result)) {
        $data = $row['data'];
    }
    $query = sprintf(SQL::$update_int_byName_preview,
        mysql_real_escape_string($data),
        mysql_real_escape_string(getUser()->getAdminId()),
        mysql_real_escape_string(getUser()->getTextEditName())
    );
    $result = mysql_query($query,$link);
    loadIntFromPreview($link, getUser()->getTextEditName());    
    echo parseTemplate(loadTemplate("editInt"));
    mysql_close($link);
    exit();
}
//rychla editace
if (isset($_POST['save']) && isset($_POST['int'])) {
    savePreview($link, $_POST['int'], getUser()->getAdminId(), getUser()->getTextEditName());
    saveChanges($link, getUser()->getTextEditName(), getUser()->getAdminId(), getUser()->getActionEdit());
}

//ulozeni preview
if (isset($_POST['int']) && isset($_POST['preview'])) {      
    savePreview($link, $_POST['int'], getUser()->getAdminId(), getUser()->getTextEditName());
    header("location: ".Settings::$rootPath."".Settings::$manager."?action=".getUser()->getActionEdit()."&preview=1");
    mysql_close($link);
    exit();
}

//potvrzeni zmen
if (isset($_POST['makeChanges'])) {
    saveChanges($link, getUser()->getTextEditName(), getUser()->getAdminId(), getUser()->getActionEdit());
}

//vraceni zpet do editace
if (isset($_POST['editChanges'])) {
    loadIntFromPreview($link, getUser()->getTextEditName());
    echo parseTemplate(loadTemplate("editInt"));
    saveSession();
    mysql_close($link);
    exit();
}

//zacatek editace
if (isset($_POST["name"]) && isset($_POST["page"])) {
    editStart($link, $_POST["name"], $_POST["page"]);    
}
//editace z editTypeResolution
if (getuser()->issetProperty("page") && getuser()->issetProperty("templateValue")) {
    $value = getUser()->removeProperty("page");
    $name = getUser()->removeProperty("templateValue");
    editStart($link, $name, $value);
}

function editStart($link, $name, $page) {
    $name = $_POST["name"];
    loadIntFromPreview($link, $name);    
    echo parseTemplate(loadTemplate("editInt"));
    getUser()->setTextEditName($name);
    getUser()->setActionEdit($_POST["page"]);
    getUser()->setProperty("sideBarResolution", "editIntResolution.php");
    saveSession();
    mysql_close($link);
    exit();
}

function loadIntFromPreview($link,$name){
    $query = sprintf(SQL::$select_int_byName_preview,
        mysql_real_escape_string($name)
    );
    $result = mysql_query($query,$link);
    global $int;
    while ($row = mysql_fetch_assoc($result)) {
        $int = $row['data'];
    }
    $query = sprintf(SQL::$select_intHistory_byName,
        mysql_real_escape_string($name,$link)
    );
    $result = mysql_query($query);
    global $historyAll;
    $historyAll = '<table><thead><tr><td>DATE</td><td>DATA</td><td>EDITED BY</td><td>LOAD THIS HISTORY</td></thead><tbody>';
    $historyEnd = '</tbody></table>';
    while ($row = mysql_fetch_assoc($result)) {
        $historyAll = $historyAll.'<tr><td>'.$row['date'].'</td><td>'.$row['data'].'</td><td>'.$row['login'].'</td><td><form method="post" action="editIntResolution.php"><input type="hidden" name="id" value="'.$row['id'].'"> <input name="loadHistory" type="submit" value="Load this history"></form></td></tr>';
    }
    $historyAll = $historyAll.$historyEnd;
}

function savePreview($link, $editor, $adminId, $textEditName) {
    $query = sprintf(SQL::$update_int_byName_preview,
        mysql_real_escape_string($editor),
        mysql_real_escape_string($adminId),
        mysql_real_escape_string($textEditName)
    );
    $result = mysql_query($query,$link);    
}

function saveChanges($link, $textEditName, $adminId, $action) {    
    $query = sprintf(SQL::$select_int_byName,
        mysql_real_escape_string($textEditName)
    );
    $result = mysql_query($query);
    while ($row = mysql_fetch_assoc($result)) {
        $oldText = $row['data'];
    }
    $query = sprintf(SQL::$select_int_byName_preview,
        mysql_real_escape_string($textEditName)
    );
    $result = mysql_query($query);
    while ($row = mysql_fetch_assoc($result)) {
        $newText = $row['data'];
    }
    $query = sprintf(SQL::$insert_intHistory,
        mysql_real_escape_string($textEditName),
        mysql_real_escape_string($oldText),
        mysql_real_escape_string($adminId)
    );
    $result = mysql_query($query);
    $query = sprintf(SQL::$update_int_byName_current,
        mysql_real_escape_string($newText),
        mysql_real_escape_string($adminId),
        mysql_real_escape_string($textEditName)
    );
    $result = mysql_query($query);   
    if (getUser()->issetProperty("addingItem")) {
        getUser()->setProperty("backFromEdit", "true");
        header("location: " . Settings::$rootPath . "" . Settings::$typeResolutionArray[3]);
    }else{
        header("location: ".Settings::$rootPath."".Settings::$manager."?action=".$action);
    }    
    getUser()->removeProperty("sideBarResolution");
    saveSession();
    mysql_close($link);
    exit();
}

mysql_close($link);

?>